#include <Misc.au3>
#include <ScreenCapture.au3>
#include <..\utility.au3>

HotKeySet("{PAUSE}", "theExit")
HotKeySet("9","theExit")

$voodooSlot = 1
$spiritWalkSlot = 2
$dogSpellSlot=3
$gargantuanSlot=4



screenshot("start")

While 1
	processkey('70', "mousePos") ;F1
	processkey('71', "leorics") ;F2
	processkey('72', "getLegendaries") ;F3
	processkey('73', "repair") ;F4
	processkey('74', "findring") ;F5
	processkey('75', "findcolor") ;F6
	processkey('76', "checkdead") ;F7
	processkey('77', "castSpelltest") ;F8
	processkey('78', "checkdisco") ;F9
 WEnd

Func castSpelltest()
	castSpell(2)
EndFunc

Func leorics()
While 1
	Sleep($lagfactor)

	;Log Run Start
	logmessage("Starting Run " & $runs)
	$runs = $runs + 1

	;Debug
	debugscreenshot()

	;Randomly take a break
	If $runs > 10 Then
		takebreak()
	EndIf

	;Repair if needed
	repair()

	;Summon dogs
	castSpell($dogSpellSlot)
	fuzzysleep(100)

	;Move to portal
	fuzzyclick("left", 146, 89)
	fuzzysleep(Random(5400,6000) * $runspeed)

	;Debug
	debugscreenshot()

	;Get shrine
	;getshrine")

	;Move to stairs
	fuzzyclick("left", 150, 0)
	fuzzysleep(2000 * $runspeed)

	;Start walking to door
	fuzzyclick("left", 150,0)
	fuzzysleep( 1400 * $runspeed)

	;Jaunt
	castSpell( $spiritWalkSlot)
	fuzzyclick("left", 150,0)
	fuzzysleep( 1400 * $runspeed)

	;Keep walking to door
	fuzzyclick("left", 150, 0)
	fuzzysleep( 3000 * $runspeed)

	;Enter door
	If enterdoor() = 0 Then
		exitgame( 0)
		ContinueLoop
	EndIf

	;If ressurect fired from courtyard, log
	If checkSpiritVesselBuff() = 1 Then
		$statusRessurectOnManorEntrance = 1
	EndIf

	Sleep($lagfactor)

	;Move down aisle ; 243,32
	fuzzyclick("left", 230, 0)
	fuzzysleep(Random(800,1200) * $runspeed)

	;Move down aisle ; 243,32
	fuzzyclick("left", 230, 0)
	fuzzysleep(Random(2800,3200,1) * $runspeed)

	;Debug
	debugscreenshot()

	;Check if we are already dead
	If checkdead( 0) = 1 Then
		exitgame( 0)
		ContinueLoop
	 EndIf

    ;Summon Gargantuan
	castSpell( $gargantuanSlot)
	fuzzysleep( Random(100,400,1))

	;Big Bad Voodo
	castSpell( $voodooSlot)
	fuzzysleep(Random(300,500,1))

	;Summon Fetish Army
	Send("{SHIFTDOWN}")
	fuzzysleep(200)
	fuzzyclick( "left", Random(860,960,1), Random(440,540,1))
	fuzzysleep(200)
	Send("{SHIFTUP}")
	fuzzysleep( Random(300,500,1))

	;Move mouse to enemies
	;fuzzymove( 455,109)

	;Fire forward
	For $i = 0 To Random(6,10,1)
		fuzzyclick( "right", Random(300,455,1), Random(109,200,1))
		fuzzysleep( Random(200,400,1))
    Next

    ;Move down aisle ; 243,32
	Send("{CTRLDOWN}")
	fuzzyclick("left", 860, 490)
	fuzzysleep(Random(200,400,1))
	Send("{CTRLUP}")

	;Debug
	debugscreenshot()

	;Fire forward
	For $i = 0 To Random(6,10,1)
		fuzzyclick( "right", Random(300,800,1), Random(109,200,1))
		fuzzysleep( Random(200,600,1))
		;Summon Gargantuan
		castSpell( $gargantuanSlot)
		;Summon Dogs
		castSpell( $dogSpellSlot)
	Next

	;Debug
	debugscreenshot()

   ;Fire at remaining guys
	For $i=0 to 10
		If checkdead( 0) = 1 Then
			ExitLoop
		EndIf

		;Summon Gargantuan
		castSpell( $gargantuanSlot)
		;Summon Dogs
		castSpell( $dogSpellSlot)

		Local $finalmobs = PixelSearch(400,0,1400,700,0xF70000,15)
		if IsArray($finalmobs) Then
			fuzzyclick( "right", $finalmobs[0],$finalmobs[1]+20)
			fuzzysleep(Random(200,600,1))
		Else
			ExitLoop
		EndIf
	Next

	;Debug
	debugscreenshot()

	;Pick up Legendaries
	getLegendaries( 0)

	;Check if we are dead and just exit
	If checkdead( 0) = 1 Then
		fuzzyclick( "left", 0, 0)
		fuzzysleep( Random(500,1000))
		exitgame( 0)
		ContinueLoop
	 EndIf

    ;Summon more dogs and gargantuan
	fuzzysleep( Random(600,1000,1))
	castSpell( $dogSpellSlot)
	fuzzysleep( Random(300,500,1))
	castSpell( $gargantuanSlot)
	fuzzysleep( Random(300,500,1))

	;Debug
	debugscreenshot()

	;Pick up rare items -- Skip this cause they suck
	;If findring() = 1 Then
	;	findring()
	;EndIf

	;Move up a smidge to pick up any stray gold
	; (if there's no yellows/legendaries, we might never have moved)
	Send("{CTRLDOWN}")
	fuzzyclick( "left", 455, 109)
	Sleep(Random(50,100))
	Send("{CTRLUP}")

	;Debug
	debugscreenshot()

    ;find and pickup piles of gold
	pickupGold()

	exitgame()

	;Restart Script
WEnd

EndFunc

Func enterdoor()
	Local $color = 0xFBFB89

	$extralag = $lagfactor / 100

	;Enter door
	fuzzyclick("left", 993, 311)
	For $j=0 to (20 + $extralag)
		Sleep(100)

		;Did we enter the manor?
		If PixelGetColor(725,609) = 0xFFFFFF AND PixelGetColor(804,610) = 0xFFFFFF AND PixelGetColor(1146,611) = 0xFFFFFF Then
			$statusEnteredManor=1
			logmessage("Entered Leoric's Manor.")
			Return 1
		EndIf
	Next

	For $i=0 to 3
		;Exit early if we are dead
		If checkdead( 0) = 1 Then
			Return 0
		EndIf

		Local $arr = PixelSearch(0,0,960,540,$color, 10)
		If IsArray($arr) Then
			fuzzyclick( "left", $arr[0], $arr[1])
			fuzzysleep( 1000)

			For $j=0 to (20 + $extralag)
				Sleep(100)

				;Did we enter the manor?
				If PixelGetColor(725,609) = 0xFFFFFF AND PixelGetColor(804,610) = 0xFFFFFF AND PixelGetColor(1146,611) = 0xFFFFFF Then
					logmessage( "Entered Leoric's Manor.")
					$statusEnteredManor=1
					Return 1
				EndIf
			Next

			;MsgBox(0,"test","test")

		EndIf

		;MsgBox(0,"test2","test2")
		;Keep moving up and left
		fuzzyclick( "left", 200, 0)
		Sleep(500)
	Next



	;We didn't, need to restart game
	logmessage( "FAILED TO ENTER Leoric's Manor. Exiting game.")
	screenshot("FAILED TO ENTER Leoric's Manor. Exiting game.")
	Return 0
EndFunc
